function getBorrowInfo() {
    $.ajax({
        url: "/reader/getBorrowInfo",
        type: "POST",
        success: function (data) {
            var tbody = document.querySelector("#datatables tbody");
            tbody.innerHTML = "";
            var rows = data.map(function (borrowInfo) {
                return `
            <tr>
                <td>${borrowInfo.readId || ""}</td>
                <td>${borrowInfo.bookId || ""}</td>
                <td>${borrowInfo.borrowdate || ""}</td>
                <td>${borrowInfo.duedate || ""}</td>
            </tr>
        `;
            });
            tbody.innerHTML = rows.join("");
        },
        error: function () {
            alert('请求发生错误，请稍后再试！');
        }
    })
}

$("#showPopup,#showPopup1").click(function () {
    $("#inputbookId1").val("");
    $("#count").val("");
    $("#inputbookId").val("");

});

document.addEventListener("DOMContentLoaded", function () {
    var input = document.getElementById("inputreadId");
    var input1 = document.getElementById("inputreadId1");
    $.ajax({
        url: "/reader/getReadId",
        type: "POST",
        success: function (readId) {
            input.value = readId;
            input1.value = readId;
        },
        error: function (error) {
            console.log("请求出错：", error);
        }
    });
});

$('#loginButton').click(function (event) {
    event.preventDefault();
    var readId = $('#inputreadId').val();
    var bookId = $('#inputbookId').val();
    $.ajax({
        type: 'POST',
        url: '/reader/returnBook',
        contentType: 'application/x-www-form-urlencoded',
        data: { readId: readId, bookId: bookId },
        success: function (response) {
            if (response === '1') {
                alert("无该书借阅记录");
            } else if (response === '2') {
                alert("图书已归还，超时归还记录罚款");
                window.location.href = "./punishInfoHtml"
            } else if (response === '3') {
                alert("图书已归还，无超时归还记录");
                getBorrowInfo();
            }else {
                alert('未知的返回结果，请联系管理员！');
            }
        },
        error: function () {
            alert('请求发生错误，请稍后再试！');
        }
    });
});

$("#btn-renew").click(function(event){
    event.preventDefault();
    $.ajax({
        url : '/reader/renewBook',
        type : 'POST',
        data : {
            readId : $("#inputreadId1").val(),
            bookId : $("#inputbookId1").val(),
            check : $("#check").val(),
            count : $("#count").val()
        },
        success: function (res) {
            if(res === 'success') {
                alert("续借成功");
                getBorrowInfo();
            }else if(res === 'notExist'){
                alert("无该书借阅记录");
            }else {
                alert("续借失败,请联系管理员！");
            }
        },
        error: function () {
            alert('请求发生错误，请稍后再试！');
        }
    })
})

